﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UMFramework.Network;

namespace UM_SERVER.Packet
{
    public class BaseInPackage : InPackage
    {
        /// <summary>
        ///     存放包头数据
        /// </summary>
        public Dictionary<String, byte[]> DIC_CONTENT = new Dictionary<string, byte[]>();
        /// <summary>
        ///     所有网络输入包基类
        /// </summary>
        public BaseInPackage()
        {
            this.InitPackage();
        }

        /// <summary>
        ///     所有网络输入包基类
        /// </summary>
        /// <param name="inpackage" type="UM_SERVER.Packet.InPackage">
        ///     <para>
        ///         输入包
        ///     </para>
        /// </param>
        public BaseInPackage(InPackage inpackage)
        {
            this.Body = inpackage.Body;
            this.InitPackage();
        }
        /// <summary>
        ///     加密包体
        /// </summary>
        public override byte[] EncryptBody()
        {
            throw new Exception("");
        }
        /// <summary>
        ///     解密包体
        /// </summary>
        public override byte[] DecryptBody()
        {
            throw new Exception("");
        }
        /// <summary>
        ///     得到包HASHCODE
        /// </summary>
        /// <returns>
        ///    返回包HASHCODE
        /// </returns>
        public override string GetHashCode()
        {
            return this.Header.Split(new String[] {Network_OrderFlag.ORDER_CENTER_SPLIT}, StringSplitOptions.RemoveEmptyEntries)[5];
        }
        /// <summary>
        ///     得到包协议
        /// </summary>
        /// <returns>
        ///     返回包协议
        /// </returns>
        public override Protocol.NetworkProtocol GetProtocol()
        {
            return Packet.Protocol.NetworkProtocol.Base;
        }
        /// <summary>
        ///     返回包加密起始位置
        ///     如果包未加密，或者是未知包则返回 -1
        /// </summary>
        /// <returns>
        ///     A int value...
        /// </returns>
        public override int GetcryptograshStart()
        {
            return -1;
        }
        // <summary>
        ///     得到登录名
        /// </summary>
        /// <returns>
        ///     返回用户名
        /// </returns>
        public String GetUsername()
        {
            int Offset = 0;
            byte[] UsernameLength = new byte[4];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, UsernameLength, 0, 4);
            Offset += 4;
            byte[] Username = new byte[BitConverter.ToInt32(UsernameLength, 0)];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, Username, 0, Username.Length);
            return Encoding.Default.GetString(Username);
        }
        /// <summary>
        ///     得到用户IP
        /// </summary>
        /// <returns>
        ///     返回用户IP
        /// </returns>
        public String GetUserIpAddress()
        {
            return Encoding.Default.GetString(this.DIC_CONTENT["IPADDRESS"]);
        }
        /// <summary>
        ///     得到聊天端口
        /// </summary>
        /// <returns>
        ///     返回聊天端口
        /// </returns>
        public int GetTalkPort()
        {
            return BitConverter.ToInt32(this.DIC_CONTENT["TALKPORT"], 0);
        }

        /// <summary>
        ///     得到包类型
        /// </summary>
        /// <returns>
        ///     返回包类型
        /// </returns>
        public override string GetType()
        {
            return Encoding.Default.GetString(this.Body["TYPE"]).Replace("*@^@*", "");
        }

        /// <summary>
        ///     初始化包数据
        /// </summary>
        public override void InitPackage()
        {
            
        }
    }
}
